package com.example.demo.mapper;

import com.example.demo.enumeration.ItemStatus;
import com.example.demo.pojo.vo.ItemListVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

@Mapper
public interface ViewHistoryMapper {
    @Select("select id from view_history where user_id = #{userId} and item_id = #{itemId}")
    Long selectIdByIds(Long userId, Long itemId);

    @Insert("insert into view_history (user_id, item_id, create_time, update_time) values (#{userId}, #{itemId}, now(), now())")
    boolean addViewHistory(Long userId, Long itemId);

    @Update("update view_history set update_time = now() where id = #{id}")
    boolean updateTimeById(Long id);

    @Delete("delete from view_history where user_id = #{userId} and item_id = #{itemId}")
    boolean deleteViewHistory(Long userId, Long itemId);

    @Delete("delete from view_history where item_id = #{itemId}")
    void deleteViewHistoryByItemId(Long itemId);

    @Select("select item.id, name, price, status, (select url from item_image where item_id = item.id order by id limit 1) as image, item.create_time, item.update_time " +
            "from item join view_history on item.id=view_history.item_id " +
            "where view_history.user_id = #{userId} order by view_history.update_time desc")
    @Result(property = "status", column = "status", javaType = ItemStatus.class)
    Page<ItemListVO> selectViewHistoryByUserId(Long userId);
}
